﻿using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage.Pickers;

namespace DNFImagePacks2Manager.Helpers
{
    // 对话框工具
    public sealed class DialogHelper
    {
        // 打开一个确认对话框
        public static async Task<ContentDialogResult> ShowConfirmDialog(Page page, string title, object content)
        {
            ContentDialog dialog = new()
            {
                Title = title,
                Content = content,
                PrimaryButtonText = "确定",
                SecondaryButtonText = "取消",
                RequestedTheme = page.ActualTheme,
                XamlRoot = page.Content.XamlRoot
            };

            return await dialog.ShowAsync();
        }

        public static ContentDialog CreateNormalDialog(Page page, string title, object content)
        {
            return new()
            {
                Title = title,
                Content = content,
                CloseButtonText = "关闭",
                RequestedTheme = page.ActualTheme,
                XamlRoot = page.Content.XamlRoot
            };
        }

        // 打开一个普通的对话框
        public static async void ShowNormalDialog(Page page, string title, object content)
        {
            await CreateNormalDialog(page, title, content).ShowAsync();
        }

        // 打开一个成功提示的对话框
        public static void ShowSuccessMessage(Page page, object message)
        {
            ShowNormalDialog(page, "成功", message);
        }

        // 打开一个错误提示的对话框
        public static void ShowErrorMessage(Page page, object message)
        {
            ShowNormalDialog(page, "错误", message);
        }

        public static async Task<string?> ShowSavePicker(Page page, string suggestName, string fileType, string suffix)
        {
            var savePicker = new FileSavePicker();
            savePicker.FileTypeChoices.Add(fileType, new List<string> { suffix });
            savePicker.SuggestedFileName = suggestName + suffix;

            savePicker.SuggestedStartLocation = PickerLocationId.Downloads;

            var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(App.GetMainWindow());
            WinRT.Interop.InitializeWithWindow.Initialize(savePicker, hWnd);

            var selectFile = await savePicker.PickSaveFileAsync();
            if (selectFile == null)
            {
                return null;
            }
            else
            {
                return selectFile.Path;
            }
        }

        public static async void ShowSavePickerAndSaveFile(Page page, string suggestName, string fileType, string suffix, string originalFile)
        {
            string? path = await ShowSavePicker(page, suggestName, fileType, suffix);
            if (path == null)
            {
                return;
            }

            try
            {
                string? dir = Path.GetDirectoryName(path);
                if (dir != null && !Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                File.Copy(originalFile, path);

                ContentDialogResult result = await DialogHelper.ShowConfirmDialog(
                        page,
                        "文件导出成功",
                        "是否打开导出目录"
                    );
                if (result == ContentDialogResult.Primary)
                {
                    try
                    {
                        ConsoleHelper.OpenFolderAndSelectFile(path);
                    }
                    catch (Exception) { }
                }
            }
            catch (Exception ex)
            {
                ShowErrorMessage(page, $"保存文件失败: {ex.Message}");
            }
        }

        public static async void ShowSavePickerAndExportZip(Page page, string suggestName, Dictionary<string, string> compressFiles)
        {
            string? path = await ShowSavePicker(page, suggestName, "ZIP 压缩文件", ".zip");
            if (path == null)
            {
                return;
            }

            MainWindow? mainWindow = App.GetMainWindow();
            if (mainWindow == null)
            {
                DialogHelper.ShowErrorMessage(page, "主窗口获取失败，请重启应用尝试");
            }
            else
            {
                mainWindow.ChangeProcessExportPage(compressFiles, path);
            }
        }
    }
}
